// pages/cart/cart.js
var goods = require('../../service/api.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        totalAmountShow: '0.00',
        totalAmount: 0.00, //总金额
        totalCount: 0, //选中的总条数
        totalNumber: 0, //选中的总数量
        carts: [],
        isEdit: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var phone = '18520314190';
        goods.cartList(phone).then(resp => {
            if (resp && resp.msg == 'SUCCESS') {
                var carts = resp.data;
                carts.map(item => {
                    item.selected = false;
                    item.isDeleted = false;
                    item.sellPrice = item.sku ? item.sku.sellPrice : item.prod.sellPrice;
                    item.totalAmount = item.prodNum * item.sellPrice;
                    var prod = item.prod;
                    if (prod.prodImage) {
                        prod.prodImages = prod.prodImage.split(',');
                        prod.firstProdImage = prod.prodImages[0];
                    }
                });
                this.setData({
                    'carts': carts
                });
                console.log(this.data);
            }
        });
    },
    detail(e) {
        console.log(goods);
        var goods = e.currentTarget.dataset.cart.prod;
        wx.setStorageSync('goods', goods)
        wx.navigateTo({
            url: '/pages/goods/detail'
        });
    },
    toggleEdit() {
        this.setData({
            isEdit: !this.data.isEdit
        });
    },
    toggleSelect(e) {
        var totalCount = this.data.totalCount,
            totalNumber = this.data.totalNumber,
            totalAmount = this.data.totalAmount,
            index = e.currentTarget.dataset.index,
            cart = e.currentTarget.dataset.cart;

        cart.selected = !cart.selected;
        if (cart.selected) {
            totalCount += 1;
            totalNumber += cart.prodNum;
            totalAmount += cart.totalAmount;
        } else {
            totalCount -= 1;
            totalNumber -= cart.prodNum;
            totalAmount -= cart.totalAmount;
        }

        var t = 'carts[' + index + ']';
        this.setData({
            [t]: cart,
            totalAmountShow: totalAmount.toFixed(2),
            totalAmount: totalAmount,
            totalCount: totalCount,
            totalNumber: totalNumber
        });
    },
    toggleSelectAll(e) {
        var carts = this.data.carts,
            totalCount = this.data.totalCount,
            totalNumber = 0,
            totalAmount = 0;
        var isAllSelected = carts.length != totalCount;
        console.log('isAllSelected', isAllSelected);
        carts.map(item => {
            item.selected = isAllSelected;
            totalNumber += item.prodNum;
            totalAmount += item.totalAmount;
        });
        totalCount = carts.length;
        if (!isAllSelected) {
            totalCount = totalAmount = totalNumber = 0;
        }
        this.setData({
            carts: carts,
            totalAmountShow: totalAmount.toFixed(2),
            totalAmount: totalAmount,
            totalCount: totalCount,
            totalNumber: totalNumber
        });
    },
    deleteSelected() {
        var carts = this.data.carts.filter(item => !item.selected)
        this.setData({
            carts: carts,
            totalAmountShow: '0.00',
            totalAmount: 0,
            totalCount: 0,
            totalNumber: 0
        });
    },
    paySelected() {

    },
    inputNum(e) {
        console.log(e);
        var index = e.currentTarget.dataset.index;
        var cart = e.currentTarget.dataset.cart;
        var t = 'carts[' + index + ']';
        cart.prodNum = e.detail.value;
        cart.prodNum = this.getNumber(cart.prodNum);
        this.setData({
            [t]: cart
        });
    },
    getNumber(num) {
        if (num <= 0) return 1;
        return num;
    },
    decreaseNumber(e) {
        var index = e.currentTarget.dataset.index;
        var cart = e.currentTarget.dataset.cart;
        var t = 'carts[' + index + ']';
        cart.prodNum = this.getNumber(--cart.prodNum);
        this.setData({
            [t]: cart
        });
    },
    increaseNumber(e) {
        var index = e.currentTarget.dataset.index;
        var cart = e.currentTarget.dataset.cart;
        cart.prodNum = this.getNumber(++cart.prodNum);
        var t = 'carts[' + index + ']';
        this.setData({
            [t]: cart
        });
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})